<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" append-to-body :v-if="dialogVisible" :visible.sync="dialogVisible" width="520px" top="10vh" @close='close'>
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" label-width="120px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('用户组名')" prop="name">
          <el-input v-emoji v-model="record.name" ref="recordName" style="width:300px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('所属用户组')" prop="father_group">
          <el-select size="small" ref="fatherGroup" v-model="record.father_group" style="width:300px;">
            <el-option v-for="item in fgroupList" :key="item.name" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('IP类型')" prop="ip_type">
          <el-select size="small" ref='ipType' v-model="record.ip_type" style="width:300px;">
            <el-option v-for="item in ipTypeList" :key="item" :label="item" :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('描述')" prop="description">
          <el-input v-emoji v-model="record.description" ref="description" style="width:300px;" size='small' :autosize="{ minRows: 4 }" type="textarea" @keydown.native="handleKeyCode($event)"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="innerAddSubmitGroup">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="innerAddCloseGroup">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getUserArray, addGroup } from '@/api/user/user'
import { inputClass, texttextClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'GroupDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (!String(value).trim()) {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        name: '',
        father_group: '',
        ip_type: 'ipv4',
        description: ''
      },
      recordCopy: {
        name: '',
        father_group: '',
        ip_type: 'ipv4',
        description: ''
      },
      ipTypeList: ['ipv4', 'ipv6'],
      fgroupList: [],
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  watch: {
    'record.description': {
      handler (newVal) {
        this.record.description = newVal.replace(/[\r\n]/g, '')
      },
      immediate: true
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['recordName']
        const textdata = ['description']
        const selectData = ['fatherGroup', 'ipType']
        inputClass(this.$refs, data)
        texttextClass(this.$refs, textdata)
        selectClass(this.$refs, selectData)
      })
      getUserArray().then(res => {
        if (res.status === 0) {
          this.fgroupList = res.info
          this.record.father_group = res.info[0].name
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addGroup(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.$emit('userListData', this.record.name)
              this.close()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.fgroupList = []
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    handleKeyCode (event) {
      if (event.keyCode === 13) {
        event.preventDefault() // 阻止浏览器默认换行操作
        return false
      }
    }
  }
}
</script>
